#include "CLogWriterServer.h"
#include "CTableManager.h"
#include "CLogBatchCreater.h"

CLogWriterServer::CLogWriterServer():
	m_tableManager(nullptr)
{

}	

void CLogWriterServer::setTableManager(CTableManager* tableManager)
{
	m_tableManager = tableManager;
}

int CLogWriterServer::WriteLog(const std::string& log)
{
	CLogBatchPtr logBatchPtr = CLogBatchCreater().CreateLogBatch(log);
	CTableBasePtr tablePtr =  m_tableManager->GetTable(logBatchPtr->GetTableName());
	tablePtr->beforeWriteLog(logBatchPtr);

	int nRet = parseLog(logBatchPtr);
	return nRet;
}

int CLogWriterServer::parseLog(CLogBatchPtr logBatch)
{
	std::string tableName = logBatch->GetTableName();

	if ( m_tableManager && m_tableManager->HasTable(tableName) )
	{
		CTableBasePtr tablePtr = m_tableManager->GetTable(tableName);
		if ( tablePtr )
		{
			return tablePtr->writeLog(logBatch);
		}
	}
	else
	{
		return -1; // Table not found
	}

	return 0;
}